//
//  LiXiView.m
//  BrushFace
//
//  Created by timer_q on 15/12/25.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "LiXiView.h"
#import "MLPickerScrollView.h"
#import "MLDemoItem.h"
#import "IWillHelper.h"

@interface LiXiView ()<MLPickerScrollViewDataSource,MLPickerScrollViewDelegate>
{
    NSString *_lixi;
    UILabel *moneyLabel;
    UILabel *detailLabel;
    NSInteger   _selectIndex;
}
@end

@implementation LiXiView
{
    MLPickerScrollView *_pickerScollView;
    NSMutableArray *data;
    CGFloat _height;
    CGFloat _width;
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _height = frame.size.height;
        _width = frame.size.width;
        [self UIConfig];
    }
    return self;
}

- (void)UIConfig{
    self.backgroundColor = [UIColor clearColor];
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _width, _height)];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.45;
    [self addSubview:backView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenThisView)];
    [backView addGestureRecognizer:tap];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(30, (_height-(_width-60)*0.8)/2.0-20, _width-60, (_width-60)*0.8)];
    whiteView.backgroundColor = [UIColor whiteColor];
    whiteView.layer.cornerRadius = 8;
    whiteView.clipsToBounds = YES;
    [self addSubview:whiteView];
    
    CGFloat whiteHeight = whiteView.frame.size.height;
    CGFloat whiteWidth = whiteView.frame.size.width;
    
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, whiteHeight/6.0, whiteWidth, 0.5)];
    topLine.backgroundColor = [UIColor lightGrayColor];
    [whiteView addSubview:topLine];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, whiteWidth, whiteHeight/6.0)];
    title.text = @"愿意支付的利息";
    title.textColor = ColorDefaultOrange;
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:15];
    [whiteView addSubview:title];
    
    UIButton *noButton = [[UIButton alloc] initWithFrame:CGRectMake(0, whiteHeight-whiteHeight/5.0, whiteWidth/2.0, whiteHeight/5.0)];
    [noButton setBackgroundImage: [UIImage imageNamed:@"whiteButton"] forState:UIControlStateNormal];
    [noButton setTitle:@"暂不修改" forState:UIControlStateNormal];
    [noButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [noButton addTarget:self action:@selector(hiddenThisView) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:noButton];
    
    UIButton *sureButton = [[UIButton alloc] initWithFrame:CGRectMake(whiteWidth/2.0, whiteHeight-whiteHeight/5.0, whiteWidth/2.0, whiteHeight/5.0)];
    [sureButton setBackgroundImage: [UIImage imageNamed:@"RedButtonBackView"] forState:UIControlStateNormal];
    [sureButton setTitle:@"确认修改" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:sureButton];
    
    CGFloat pickHeight = whiteHeight - whiteHeight/7.0 - whiteHeight/5.0;
    
    moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, whiteHeight/7.0, whiteWidth, pickHeight*0.4)];
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    moneyLabel.text  = @"¥44.00";
    moneyLabel.font = [UIFont systemFontOfSize:30];
    moneyLabel.textColor = ColorDefaultOrange;
    [whiteView addSubview:moneyLabel];
    
    _selectIndex = 8;
    detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, whiteHeight/7.0+2*pickHeight*0.4, whiteWidth, pickHeight*0.2)];
    detailLabel.textAlignment = NSTextAlignmentCenter;
    detailLabel.text  = @"¥系统推荐支付的日息: 0.08%";
    detailLabel.font = [UIFont systemFontOfSize:12];
    detailLabel.textColor = ColorDefaultOrange;
    [whiteView addSubview:detailLabel];
    
    // 1.数据源
    data = [NSMutableArray array];
    NSMutableArray *titleArray = [NSMutableArray array];
    for (int i=0; i<101; i++) {
        [titleArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    for (int i = 0; i < titleArray.count; i++) {
        NSString *model = [titleArray objectAtIndex:i];
        [data addObject:model];
    }
    CGFloat kItemH = pickHeight*0.4;
    // 2.初始化
    _pickerScollView = [[MLPickerScrollView alloc] initWithFrame:CGRectMake(0, whiteHeight/7.0+kItemH, whiteWidth, kItemH)];
    _pickerScollView.backgroundColor = [UIColor whiteColor];
    _pickerScollView.itemWidth = _pickerScollView.frame.size.width / 5;
    _pickerScollView.itemHeight = kItemH;
    _pickerScollView.firstItemX = (_pickerScollView.frame.size.width - _pickerScollView.itemWidth) * 0.5;
    _pickerScollView.dataSource = self;
    _pickerScollView.delegate = self;
    [whiteView addSubview:_pickerScollView];
    
    // 3.刷新数据
    [_pickerScollView reloadData];
    
    _pickerScollView.seletedIndex = _selectIndex;
    [_pickerScollView scollToSelectdIndex:_selectIndex];
    _lixi = [NSString stringWithFormat:@"%ld",_selectIndex];
    [IWillHelper sharedInstance].couponsid = _lixi;
}

#pragma mark - dataSource
- (NSInteger)numberOfItemAtPickerScrollView:(MLPickerScrollView *)pickerScrollView
{
    return data.count;
}

- (MLPickerItem *)pickerScrollView:(MLPickerScrollView *)pickerScrollView itemAtIndex:(NSInteger)index
{
    // creat
    MLDemoItem *item = [[MLDemoItem alloc] initWithFrame:CGRectMake(0, 0, pickerScrollView.itemWidth, pickerScrollView.itemHeight)];
    
    // assignment
    NSString *model = [data objectAtIndex:index];
    item.title = model;
    [item setGrayTitle];
    // tap
    item.PickerItemSelectBlock = ^(NSInteger d){
        [_pickerScollView scollToSelectdIndex:d];
    };
    
    return item;
}

#pragma mark - delegate
- (void)itemForIndexChange:(MLPickerItem *)item
{
    _lixi = [NSString stringWithFormat:@"%ld",item.index];
    moneyLabel.text = [NSString stringWithFormat:@"¥%@",[IWillHelper fetchMoneyWithLixi:_lixi]];
    ITLogSP(@"%ld",item.index);
    if (item.index==_selectIndex) {
        detailLabel.text  = [NSString stringWithFormat:@"系统推荐支付的日息: %.2f%%",_selectIndex/100.0];
    }else{
        detailLabel.text  = [NSString stringWithFormat:@"您愿意支付的日息: %.2f%%",item.index/100.0];
    }
    [item changeSizeOfItem];
}

- (void)itemForIndexBack:(MLPickerItem *)item
{
    [item backSizeOfItem];
}

#pragma mark - action
- (void)hiddenThisView{
    self.hidden = YES;
    _pickerScollView.seletedIndex = 8;
    [_pickerScollView scollToSelectdIndex:8];
}
- (void)sureButtonClick{
    [IWillHelper sharedInstance].couponsid = _lixi;
    [self.delegate lixiChangeLixi:_lixi andMoney:moneyLabel.text];
    self.hidden = YES;
}
- (void)lixiShow{
    NSInteger priod = [[IWillHelper sharedInstance].priod integerValue];
    if (priod<=1) {
        if (priod==1) {
            _selectIndex=8;
        }else{
            if ([[IWillHelper sharedInstance].days integerValue]<=10) {
                _selectIndex=10;
            }else{
                _selectIndex=8;
            }
            
        }
    }else if ([[IWillHelper sharedInstance].priod integerValue]>1&&[[IWillHelper sharedInstance].priod integerValue]<=3) {
        _selectIndex=6;
    }else if ([[IWillHelper sharedInstance].priod integerValue]>3&&[[IWillHelper sharedInstance].priod integerValue]<=6) {
        _selectIndex=5;
    }else if ([[IWillHelper sharedInstance].priod integerValue]>6&&[[IWillHelper sharedInstance].priod integerValue]<=12) {
        _selectIndex=4;
    }
    _pickerScollView.seletedIndex = _selectIndex;
    [_pickerScollView scollToSelectdIndex:_selectIndex];
    _lixi = [NSString stringWithFormat:@"%ld",_selectIndex];
    [IWillHelper sharedInstance].couponsid = _lixi;
    
    moneyLabel.text = [NSString stringWithFormat:@"¥%@",[IWillHelper fetchMoneyWithLixi:_lixi]];
}
@end
